package com.ido.wechat.component;

import com.ido.wechat.bean.*;
import com.ido.wechat.constants.Message;
import com.ido.wechat.enums.MessageEnum;
import com.ido.wechat.service.IFunction;
import com.thoughtworks.xstream.XStream;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: zoub
 * @create: 2020-01-08 15:14
 **/
public class MessageTemplate {

    public final HashMap<MessageEnum, IFunction> getFunction() {
        HashMap<MessageEnum, IFunction> map = new HashMap<>();
        map.put(MessageEnum.TEXT, this::sendTextMsg);
        map.put(MessageEnum.IMAGE, this::sendImageMsg);
        map.put(MessageEnum.VIDEO, this::sendVideoMsg);
        map.put(MessageEnum.NEWS, this::sendNewsMsg);
        return map;
    }


    /**
     * 回复文本消息
     *
     * @param fromUserName
     * @param toUserName
     * @param backMsg
     * @return
     */
    public String sendTextMsg(String fromUserName, String toUserName, String backMsg) {
        TextMessage text = new TextMessage();
        text.setFromUserName(toUserName);
        text.setToUserName(fromUserName);
        text.setMsgType(MessageEnum.TEXT.getValue());
        text.setContent(backMsg);
        text.setCreateTime(System.currentTimeMillis());
        return this.textMessageToXml(text);
    }

    /**
     * 回复图片消息
     *
     * @param fromUserName
     * @param toUserName
     * @param mediaId
     * @return
     */
    public String sendImageMsg(String fromUserName, String toUserName, String mediaId) {
        ImageMessage imageMsg = new ImageMessage();
        imageMsg.setFromUserName(toUserName);
        imageMsg.setToUserName(fromUserName);
        imageMsg.setMsgType(MessageEnum.IMAGE.getValue());
        imageMsg.setCreateTime(System.currentTimeMillis());

        Image image = new Image();
        image.setMediaId(mediaId);
        imageMsg.setImage(image);
        return this.imageMessageToXml(imageMsg);
    }

    /**
     * 回复图文消息
     *
     * @param fromUserName
     * @param toUserName
     * @return
     */
    public String sendNewsMsg(String fromUserName, String toUserName, String backMsg) {
        NewsMessage news = new NewsMessage();
        news.setFromUserName(toUserName);
        news.setToUserName(fromUserName);
        news.setMsgType(MessageEnum.NEWS.getValue());
        news.setCreateTime(System.currentTimeMillis());

        List<Articles> articles = new ArrayList<>();

        Articles article1 = new Articles();
        article1.setTitle("Ru");
        article1.setDescription("Ru");
        article1.setPicUrl(Message.newCover1);
        article1.setUrl(Message.newUrl1);
        articles.add(article1);

   /*     Articles article2 = new Articles();
        article2.setTitle("test2");
        article2.setDescription("test2");
        article2.setPicUrl("");
        article2.setUrl("");*/
        //articles.add(article2);

        news.setArticles(articles);
        news.setArticleCount(articles.size());
        return this.newsMessageToXml(news);
    }


    /**
     * 回复视频消息
     *
     * @param fromUserName
     * @param toUserName
     * @param mediaId
     * @return
     */
    public String sendVideoMsg(String fromUserName, String toUserName, String mediaId) {
        VideoMessage msg = new VideoMessage();
        msg.setFromUserName(toUserName);
        msg.setToUserName(fromUserName);
        msg.setMsgType(MessageEnum.VIDEO.getValue());
        msg.setCreateTime(System.currentTimeMillis());

        Video video = new Video();
        video.setMediaId(mediaId);
        msg.setVideo(video);
        return this.videoMessageToXml(msg);
    }


    /**
     * 文本消息对象转换成xml
     *
     * @param textMessage 文本消息对象
     * @return xml
     */
    private String textMessageToXml(TextMessage textMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", textMessage.getClass());
        return xstream.toXML(textMessage);
    }

    /**
     * 图片消息对象转换成xml
     *
     * @param imageMsg
     * @return
     */
    public String imageMessageToXml(ImageMessage imageMsg) {
        XStream xstream = new XStream();
        xstream.alias("xml", imageMsg.getClass());
        return xstream.toXML(imageMsg);
    }

    /**
     * 图文消息对象转换成xml
     *
     * @param newsMessage
     * @return
     */
    public String newsMessageToXml(NewsMessage newsMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", newsMessage.getClass());
        xstream.alias("item", new Articles().getClass());
        return xstream.toXML(newsMessage);
    }

    /**
     * 视频消息对象转换成xml
     *
     * @param videoMessage
     * @return
     */
    public String videoMessageToXml(VideoMessage videoMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", videoMessage.getClass());
        return xstream.toXML(videoMessage);
    }

}
